package com.douwong.student.popuwindow;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bumptech.glide.Glide;
import com.douwong.student.R;
import com.douwong.student.activity.HandWrittenImagView;
import com.douwong.student.enums.E_SubjectType;
import com.douwong.student.rxjava.EndPointInterface;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.FlowLayout;
import com.douwong.student.view.MTextView;

import java.util.List;

/**
 * 问答互动弹窗
 */

public class ShowQuestionAnswerDetailPopuWindow extends PopupWindow {
    private static final String TAG = ShowQuestionAnswerDetailPopuWindow.class.getSimpleName();
    private ListView mListView;
    private View view;
    private View mHeadView;
    private MTextView mTitle;
    private FlowLayout mImageRoot;
    private Context mContext;
    private ImageView mImageview;
    private ImageUrlShowPopu mImageUrlShowPopu;

    /**
     *
     * @param context
     * @param type  题目类型
     * @param body  题目
     * @param stringList  选项
     * @param filePath  图片路径
     */
    public ShowQuestionAnswerDetailPopuWindow(Context context, E_SubjectType type, String body, List<String> stringList, String filePath) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.show_new_subject_activity, null);

        mHeadView = inflater.inflate(R.layout.head_show_subject_popuwindow, null);
        initView();
        setValue(body,filePath,type,stringList);
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xffffff);
        this.setBackgroundDrawable(dw);
    }

    private void initView() {
        mTitle = (MTextView) mHeadView.findViewById(R.id.text_new_showsubject_popuwindow_title);
        mImageRoot = (FlowLayout) mHeadView.findViewById(R.id.ll_new_showsubject_popuwindow_imagroot);
        mListView = (ListView) view.findViewById(R.id.listview_newshow_subject_popu);

    }

    private void setValue(String body, final String filePath, final E_SubjectType type, final List<String> stringList) {
        //listveiw添加点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //
                dismiss();
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //判断数据类型
        switch (type) {
            case single:
                mTitle.setMText("(单选题) "+body);
                break;

            case multiple:
                mTitle.setMText("(多选题) "+body);
                break;
            case judgment:
                mTitle.setMText("(判断题)"+body);
                break;
        }

        if (filePath!=null&&!filePath.equals("")){


            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UIUtils.dip2px(150), UIUtils.dip2px(150));

            params.leftMargin = 20;
            mImageview = new ImageView(mContext);
            Glide.with(mContext).load(EndPointInterface.HEAD_PIC+filePath).placeholder(R.mipmap.answerdefault).into(mImageview);
            mImageRoot.addView(mImageview, params);
            mImageview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    /*mImageUrlShowPopu = new ImageUrlShowPopu(mContext,filePath);
                    mImageUrlShowPopu.showAtLocation(v, Gravity.CENTER,0,0);*/
                    Intent        mIntent = new Intent(mContext, HandWrittenImagView.class);
                    mIntent.putExtra("filePath",filePath);
                    mIntent.putExtra("type","handrecord");
                    mContext.startActivity(mIntent);
                }
            });

        }
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mImageUrlShowPopu!=null && mImageUrlShowPopu.isShowing()){
                    mImageUrlShowPopu.dismiss();
                }
            }
        });
        //listview显示
        mListView.addHeaderView(mHeadView);
        setListViewHeightBasedOnChildren(mListView);
        mListView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {

                if (type==E_SubjectType.judgment) {
                    return 0;
                }

                if (stringList.size() > 0) {
                    return stringList.size();
                }
                return 0;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }


            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = View.inflate(mContext, R.layout.head_show_subject_popuwindow, null);
                    holder.textViewTiele = (MTextView) convertView.findViewById(R.id.text_new_showsubject_popuwindow_title);
                    holder.mLinearLayoutRoot = (FlowLayout) convertView.findViewById(R.id.ll_new_showsubject_popuwindow_imagroot);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.textViewTiele.setTextColor(Color.parseColor("#333333"));

                holder.mLinearLayoutRoot.removeAllViews();

                holder.textViewTiele.setMText(stringList.get(position));
                return convertView;
            }
        });

    }



    //此方法在setAdapter之前调用
    private void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    private class ViewHolder {
        MTextView textViewTiele;
        FlowLayout mLinearLayoutRoot;

    }

}
